<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%


String usernameR = request.getParameter("username");
String passwordR = request.getParameter("password");
String repassword = request.getParameter("repassword");
String id = request.getParameter("id");
String fname = request.getParameter("fname");
String lastname = request.getParameter("lastname");

String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
try
{
	Class.forName(driver).newInstance();
	Connection connection = DriverManager.getConnection(url, username, password);
	Statement statement0 = connection.createStatement();
	String sql = "select * from student where id = '"+id+"'";
	ResultSet r = statement0.executeQuery(sql);
	if(!r.next())
	{
		Statement statement01 = connection.createStatement();
		String sql0 = "select * from user_log_in where username = '"+usernameR+"'";
		ResultSet r0 = statement01.executeQuery(sql0);
		if(!r0.next())
		{
			Statement statement = connection.createStatement();
			String query = "insert into student (id,first_name,last_name,proj_id) values("+id+",'"+fname+"','"+lastname+"',0)";
	
			statement.executeUpdate(query);
			String query2 = "insert into user_log_in (username,password,type,ref_id) values('"+usernameR+"','"+passwordR+"','student',"+id+")";
			statement.executeUpdate(query2);
	
		
			response.sendRedirect("index.jsp");
		} else
			response.sendRedirect("studentForm.jsp?error=username");
	} else
		response.sendRedirect("studentForm.jsp?error=found");
	connection.close();
}catch(ClassNotFoundException cnfe)
{
		out.println("<br>Couldn't find class file" + cnfe); 
}catch(SQLException sqle)
{
		response.sendRedirect("studentForm.jsp?error=yes");
}catch(InstantiationException ie)
{
		out.println("<br>Instant exception : " + ie);
}catch(IllegalAccessException iae)
{
		out.println("<br>Illegal access exception : " + iae);
		
}
%>